package com.sky.service.impl;

import com.sky.context.BaseContext;
import com.sky.dto.ShoppingCartDTO;
import com.sky.entity.Dish;
import com.sky.entity.Setmeal;
import com.sky.entity.ShoppingCart;
import com.sky.mapper.DishMapper;
import com.sky.mapper.SetmealMapper;
import com.sky.mapper.ShoppingCartMapper;
import com.sky.service.ShoppingCartService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;

@Service
public class ShoppingCartServiceImpl implements ShoppingCartService {
    @Autowired
    private ShoppingCartMapper shoppingCartMapper;
    @Autowired
    private DishMapper dishMapper;
    @Autowired
    private SetmealMapper setmealMapper;

    /**
     * 添加购物车
     *
     * @param dto
     */
    @Override
    public void add(ShoppingCartDTO dto) {
        //目标：需要将用户添加的商品存入到购物车表中shopping_cart
        //1.构造购物车对象数据
        ShoppingCart shoppingCart = new ShoppingCart();
        BeanUtils.copyProperties(dto, shoppingCart);

        //2.判断购物车中是否存在同一个商品，
        //查购物车：条件--usre_id、商品id（如果是菜品还需要加上口味条件）
        shoppingCart.setUserId(BaseContext.getCurrentId());

        List<ShoppingCart> cartList = shoppingCartMapper.getBy(shoppingCart);
        if (cartList != null && cartList.size() > 0) {
            ShoppingCart cartDB = cartList.get(0);
            // 如果存在的话，应该做更新操作，更新数量+1
            cartDB.setNumber(cartDB.getNumber() + 1);
            shoppingCartMapper.update(cartDB);
        } else {
            //3.如果不存在，做新增操作，调用mapper的新增方法，将购物车数据存入表中shopping_cart
            //补充其他属性值
            //根据商品id查询商品信息，补充商品的名称、图片、价格
            if(dto.getDishId() != null) { //代表添加的是菜品
                Dish dish = dishMapper.getById(dto.getDishId());
                shoppingCart.setName(dish.getName());
                shoppingCart.setImage(dish.getImage());
                shoppingCart.setAmount(dish.getPrice());
            }else{  //代表添加的套餐
                Setmeal setmeal = setmealMapper.getById(dto.getSetmealId());
                shoppingCart.setName(setmeal.getName());
                shoppingCart.setImage(setmeal.getImage());
                shoppingCart.setAmount(setmeal.getPrice());
            }
            shoppingCart.setNumber(1);  //设置数量
            shoppingCart.setCreateTime(LocalDateTime.now());    //创建时间
            shoppingCartMapper.insert(shoppingCart);
        }
    }

    /**
     * 查询购物车
     *
     * @return
     */
    @Override
    public List<ShoppingCart> list() {
        // 注意：只能查询自己名下的购物车
        ShoppingCart shoppingCart = ShoppingCart
                .builder()
                .userId( BaseContext.getCurrentId())
                .build();
        return shoppingCartMapper.getBy(shoppingCart);
    }

    /**
     * 清空购物车
     */
    @Override
    public void clean() {
        //注意：只能清空自己名下的购物车数据
        shoppingCartMapper.deleteByUserId(BaseContext.getCurrentId());
    }

    /**
     * 删除购物车中一个商品
     * @param shoppingCartDTO
     */
    public void subShoppingCart(ShoppingCartDTO shoppingCartDTO) {
        ShoppingCart shoppingCart = new ShoppingCart();
        BeanUtils.copyProperties(shoppingCartDTO,shoppingCart);
        //设置查询条件，查询当前登录用户的购物车数据
        shoppingCart.setUserId(BaseContext.getCurrentId());

        List<ShoppingCart> list = shoppingCartMapper.getBy(shoppingCart);

        if(list != null && list.size() > 0){
            shoppingCart = list.get(0);

            Integer number = shoppingCart.getNumber();
            if(number == 1){
                //当前商品在购物车中的份数为1，直接删除当前记录
                shoppingCartMapper.deleteById(shoppingCart.getId());
            }else {
                //当前商品在购物车中的份数不为1，修改份数即可
                shoppingCart.setNumber(shoppingCart.getNumber() - 1);
                shoppingCartMapper.update(shoppingCart);
            }
        }
    }
}
